<?php

/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 7/8/14
 * Time: 8:51 AM
 */
class GetFatWalletCommand extends CConsoleCommand {
    public function run($args) {
        $url = "http://www.fatwallet.com/";
        $date = date('d-m-Y');

        $dom = new simple_html_dom();
        $dom->load(HSCommon::curl($url));
        if ($dom == null) return;
        $wrappers = $dom->find('div[class="offer-cell"]');
        $results = array();
        foreach ($wrappers as $wrapper) {
            //handle image
            $img_wrapper = $wrapper->find('div[class="offer-image"]', 0);

            if ($img_wrapper != null) {
                $img = $img_wrapper->find('img', 0);
            }

            //handle title and link
            $title_wrapper = $wrapper->find('a[class="offer-details-url"]', 0);
            if ($title_wrapper == null) {
                continue;
            }

            //handle price
            $price_wrapper = $wrapper->find('span[class="current-price"]', 0);

            if ($price_wrapper == null) {
                continue;
            }

            $deal = new Deal();
            $deal->picture = $img_wrapper == null ? "" : str_replace('//static', 'http://static', $img->attr['data-original']);
            $deal->name = trim($title_wrapper->plaintext);
            $deal->url = "http://fatwallet.com" . trim($title_wrapper->href);
            $deal->price = trim($price_wrapper->innertext);
            $deal->created_date = $date;
            $deal->type = DealTypeEnum::fatwallet;

            $old_deal = Deal::model()->findAllByAttributes(array(
                'name' => $deal->name,
//                'created_date' => $deal->created_date,
                'type' => $deal->type
            ));
            if ($old_deal != null) continue;

            if ($deal->save()) {
                array_push($results, $deal);
            }
        }
        if (count($results) > 0 && count($results) > Yii::app()->params['limit']) {
            $html = '';
            $html .= '<table style="width:100%;" border="1">';
            $html .= '<tr>';
            $html .= '<th>Picture</th>';
            $html .= '<th>Name</th>';
            $html .= '<th>Price</th>';
            $html .= '<th>Link</th>';
            $html .= '</tr>';
            foreach ($results as $result) {

                $dom = new simple_html_dom();
                $dom->load($result->price);
                $was_price = $dom->find('span[class="was-price"]', 0);
                if ($was_price != null) {
                    $was_price->style = 'text-decoration: line-through;font-size: 10px;';
                }

                $html .= '<tr>';
                if ($result->picture != '') {
                    $html .= '<td style="text-align:center"><img style="max-width: 100px; max-height: 100px" alt="" src="' . $result->picture . '"/> </td>';
                } else {
                    $html .= '<td> </td>';
                }
                $html .= '<td> ' . $result->name . '</td>';
                $html .= '<td>' . $dom->innertext . '</td>';
                $html .= '<td><a href="' . $result->url . '">Link</a></td>';
                $html .= '</tr>';

            }
            $html .= '</table>';

            Mailer::sendEmail(Yii::app()->params['large_email'], "Fatwallet's deals", $html);
        } else {
            foreach ($results as $result) {
                //handle price
                $dom = new simple_html_dom();
                $dom->load($result->price);
                $was_price = $dom->find('span[class="was-price"]', 0);
                if ($was_price != null) {
                    $was_price->style = 'text-decoration: line-through;font-size: 10px;';
                }

                $html = '';
                $html .= '<p>';
                $html .= "<div>Name: " . $result->name . "</div>";
                $html .= "<div>Price: " . $dom->innertext . "</div>";
                $html .= "<div><a href=" . $result->url . ">Link</a></div>";
                $html .= '</p>';

                $price_title = strip_tags($result->price);
                $arr = array_filter(explode('$', $price_title));
                $arr = HSCommon::handleArray($arr);
                $price_title = count($arr) >= 2 ? "$$arr[0] | $$arr[1]" : "$$arr[0]";

                Mailer::sendEmail(Yii::app()->params['small_email'], "$result->name | $price_title", $html);
            }
        }
    }
}